package uk.co.kevinlockwood.kol.messages;

import uk.co.kevinlockwood.kol.JavaHelpers;
import uk.co.kevinlockwood.kol.droid.DroidHelpers;

public class KoLMessage {
	private String raw;

	private KoLMessage() {
		super();
	}

	@Override
	public String toString() {
		return raw;
	}

	private static String extLinkStart = "<a target=_blank href=\"";
	private static String extLinkEnd = "\"";
	private static String linkText = "<font color=blue>[link]</font>";
	private static String[] stringsToStripOffTheBeginningOfMessages = {"<br>"};
	private static String[] stringsToStripOffTheEndOfMessages = {"<br />","</font>","<br>"};
	public static KoLMessage parse(String msg) {
		KoLMessage m = new KoLMessage();
		if (msg != null) {
			try {
				msg = msg.trim();
				msg = msg.replaceAll("<hr>", "").replaceAll("<p>","<br>");
				for (String strip : stringsToStripOffTheBeginningOfMessages) {
					while (msg.startsWith(strip)) {
						msg = msg.substring(strip.length()).trim();
					}
				}
				for (String strip : stringsToStripOffTheEndOfMessages) {
					while (msg.endsWith(strip)) {
						msg = msg.substring(0,msg.length()-strip.length()).trim();
					}
				}
				while (msg.contains(linkText)) {
					String link = JavaHelpers.substringToString(msg, extLinkStart, extLinkEnd);
					// whole link should end up as <a href="$link">$link</a>
					int linkStartPos = msg.indexOf(extLinkStart);
					StringBuilder sb = new StringBuilder(msg.substring(0, linkStartPos));
					sb.append("<a href=\"").append(link).append("\">").append(link).append("</a>");
					// find end point of link gubbins
					int offset = linkStartPos + extLinkStart.length() + link.length()*2 + linkText.length() + 6; // at least here is the end of the link
					int i = msg.indexOf(" ", offset); // find the next space
					if (i > 0) sb.append(msg.substring(i));
					msg = sb.toString();
				}
			} catch (Exception e) {
				DroidHelpers.printStackTrace(e);
			} finally {
				DroidHelpers.debug("msg was [", msg, "]");
			}
		}
		m.raw = msg;
		return m;
	}
}

/* new message received:
 * ???
 */

/* haiku:
 * <font color=green>[haiku]</font> <b><a target=mainpane href="showplayer.php?who=362517"><font color=black>Vatel</font></b></a>:
 */

/*
 * <b><a target=mainpane href="showplayer.php?who=2257540"><font color=black>rooksley</font></a>:</b> sorry <a target=_blank href="http://kevinlockwood.co.uk/kol/"><font color=blue>[link]</font></a> http:// kevinlockwood.co.uk/ kol/<br>
 */

/* system msg:
 * <font color=red><b><b><a target=mainpane href="showplayer.php?who=1"><font color=red>System
 * 	Message</font></b></a>: Riff and Stuff and I will be recording the next KoL show in a bit.
 * 	Send questions/comments to radiobugbear via chat PM.<br></b></font>
 */

/* mod:
 * <font color=green><font color=green>[games]</font> <b><a target=mainpane href="showplayer.
 * 	php?who=585112"><font color=green>Mod Announcement</font></b></a>: ....<br></font>
 */

/* action:
 * <font color=green>[games]</font> <b><i><a target=mainpane href="showplayer.php?who=2103208">
 * 	<font color="black">Kalina Vulgaris</b></font></a> presses 'enter' on Emhgee</i><br>
 */

/* log on
 * <font color=green><a target=mainpane href='showplayer.php?who=2257540'>
 * 	<font color=green><b>rooksley</b></font></a> logged on.</font><br><!--lastseen:1281687716-->
 */

/* message in local room:
 * <b><a target=mainpane href="showplayer.php?who=2257540"><font color=#9933CC>rooksley</font>
 * 	</b></a>: hi there<br><!--lastseen:1281687733-->
 */